package com.qcby.llmtchstd.service.Impl;

import com.qcby.llmtchstd.common.ResponseResult;
import com.qcby.llmtchstd.dao.StudentDao;
import com.qcby.llmtchstd.entity.Student;
import com.qcby.llmtchstd.service.ClassService;
import com.qcby.llmtchstd.service.StudentService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {

    @Autowired
    private StudentDao studentDao;
    @Autowired
    private ClassService classService;

    @Override
    public ResponseResult count() {
        try {
            Integer num = studentDao.count();
            if (num != null) {
                return ResponseResult.success(num); // 查询成功，返回数量
            } else {
                return ResponseResult.fail("5001", "页码查询失败！"); // 处理结果为空的情况
            }
        }
        catch (Exception e) {
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：查询学生数量失败！"); // 统一处理数据库异常
        }
    }

    @Override
    public ResponseResult page(Integer page, Integer pageSize) {
        int offset = (page - 1) * pageSize;
        try {
            List<Student> list=studentDao.page(offset,pageSize);
            return ResponseResult.success(list);
        }
        catch (Exception e){
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：查询学生数量失败");
        }
    }

    @Override
    public ResponseResult add(Student student) {
        try{
            int num=studentDao.add(student);
            if(num==1){
                return ResponseResult.success();
            }
            return ResponseResult.fail("4003","添加学生失败，请检查数据");
        }
        catch (Exception e){
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：查询学生数量失败");
        }
    }

    @Override
    public ResponseResult search(Student student) {
        try {
            List<Student> list=studentDao.search(student);
            if(list.size()!=0){
                return ResponseResult.success(list);
            }
            return ResponseResult.fail("4003","学生不存在，请检查数据");
        }
        catch (Exception e){
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：查询学生数量失败");
        }
    }

    @Override
    public ResponseResult update(Student student) {
        try{
            int num=studentDao.update(student);
            if(num==1){
                return ResponseResult.success();
            }
            return ResponseResult.fail("4003","修改学生失败，请检查数据");
        }
        catch (Exception e){
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：修改学生数量失败");
        }
    }


    @Override
    public ResponseResult delete(Integer id) {
        try{
            int num=studentDao.delete(id);
            if(num==1){
                return ResponseResult.success();
            }
            return ResponseResult.fail("4003","删除学生失败，请检查数据");
        }
        catch (Exception e){
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：删除学生数量失败");
        }
    }

    @Override
    public ResponseResult edit(Integer id) {
        try {
            Student temp=studentDao.edit(id);
            if(temp.getName()!=null){
                return ResponseResult.success(temp);
            }
            return ResponseResult.fail("4003","学生不存在，请检查数据");
        }
        catch (Exception e){
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：查询学生数量失败");
        }
    }

    @Override
    public ResponseResult serCount(Student student) {
        try {
            Integer num = studentDao.serCount(student);
            if (num != null) {
                return ResponseResult.success(num); // 查询成功，返回数量
            } else {
                return ResponseResult.fail("5001", "页码查询失败！"); // 处理结果为空的情况
            }
        }
        catch (Exception e) {
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：查询学生数量失败！"); // 统一处理数据库异常
        }
    }

    @Override
    public ResponseResult phone(String phone) {
        try{
            int num=studentDao.phone(phone);
            if(num==0){
                return ResponseResult.success();
            }
            else if(num>0){
                return ResponseResult.fail("4004","该手机号已被注册，请重新更换手机号");
            }
            return ResponseResult.fail("4003","手机号查询失败，请检查数据");
        }
        catch (Exception e){
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：查询手机号失败");
        }
    }

    @Override
    public ResponseResult sno(String sno) {
        try{
            int num=studentDao.sno(sno);
            if(num==0){
                return ResponseResult.success();
            }
            else if(num>0){
                return ResponseResult.fail("4004","学号不可重复");
            }
            return ResponseResult.fail("4003","学号查询失败，请检查数据");
        }
        catch (Exception e){
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：查询学号失败");
        }
    }

    @Override
    public ResponseResult phoneUp(Student student) {
        try{
            int num=studentDao.phoneUp(student);
            if(num==0){
                return ResponseResult.success();
            }
            else if(num>0){
                return ResponseResult.fail("4004","该手机号已被注册，请重新更换手机号");
            }
            return ResponseResult.fail("4003","手机号查询失败，请检查数据");
        }
        catch (Exception e){
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：查询手机号失败");
        }
    }

    @Override
    public ResponseResult snoUp(Student student) {
        try{
            int num=studentDao.snoUp(student);
            if(num==0){
                return ResponseResult.success();
            }
            else if(num>0){
                return ResponseResult.fail("4004","学号不可重复");
            }
            return ResponseResult.fail("4003","学号查询失败，请检查数据");
        }
        catch (Exception e){
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：查询学号失败");
        }
    }

    @Override
    public ResponseResult countById(Integer id) {
        try {
            Integer num = studentDao.countById(id);
            if (num != null) {
                return ResponseResult.success(num); // 查询成功，返回数量
            } else {
                return ResponseResult.fail("5001", "页码查询失败！"); // 处理结果为空的情况
            }
        }
        catch (Exception e) {
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：查询学生数量失败！"); // 统一处理数据库异常
        }
    }

    @Override
    public ResponseResult pageById(Student student) {
        //offset 在js里计算完
        try {
            List<Student> list=studentDao.pageById(student);
            return ResponseResult.success(list);
        }
        catch (Exception e){
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：查询学生数量失败");
        }
    }

    @Override
    public ResponseResult searchById(Student student) {
        try {
            List<Student> list=studentDao.searchById(student);
            if(list.size()!=0){
                return ResponseResult.success(list);
            }
            return ResponseResult.fail("4003","学生不存在，请检查数据");
        }
        catch (Exception e){
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：查询学生数量失败");
        }
    }

    @Override
    public ResponseResult serCountById(Student student) {
        try {
            Integer num = studentDao.serCountById(student);
            if (num != null) {
                return ResponseResult.success(num); // 查询成功，返回数量
            } else {
                return ResponseResult.fail("5001", "页码查询失败！"); // 处理结果为空的情况
            }
        }
        catch (Exception e) {
            e.printStackTrace();//打印异常到控制台
            return ResponseResult.dbError("数据库异常：查询学生数量失败！"); // 统一处理数据库异常
        }
    }

    @Override
    public ResponseResult getStudentDetails(Integer id) {
        try {
            ResponseResult studentResult = this.edit(id);
            if (!"0000".equals(studentResult.getCode())) {
                return studentResult;
            }

            Student student = (Student) studentResult.getData();
            ResponseResult classResult = classService.edit(student.getClassId());
            if (!"0000".equals(classResult.getCode())) {
                return classResult;
            }

            com.qcby.llmtchstd.entity.Class clazz = (com.qcby.llmtchstd.entity.Class) classResult.getData();
            student.setClassName(clazz.getName()); // 设置班级名称
            return ResponseResult.success(student);
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseResult.dbError("数据库异常：获取学生详细信息失败");
        }
    }
}
